//
//  CC_Anchor_CollectionViewCell.m
//  HimalayaMountain
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CC_Anchor_CollectionViewCell.h"
#import "CCBaseCollectionView.h"

#import "CC_Custom_CollectionReusableView.h"  // 头视图
#import "CC_AnchorTitle_Model.h" // 大model
#import "CC_AnchorDetailed_Model.h" // 小model
#import "CC_ImageLabel_CollectionViewCell.h"  // 常用的cell风格
#import "CC_AnchorSinger_CollectionViewCell.h"  // 歌手cell


#define IMAGELABELCELL @"CC_ImageLabel_CollectionViewCell"
#define ANCHORSINGER @"CC_AnchorSinger_CollectionViewCell"


@interface CC_Anchor_CollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, retain) CCBaseCollectionView *collectionView;

@property(nonatomic, retain) NSMutableArray *anchorTitleModelArray;  // 装大model的数组;


@end


@implementation CC_Anchor_CollectionViewCell

- (void)dealloc
{
    [_anchorTitleModelArray release];
    [_collectionView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self handleData];
        [self createCollectionView];
        
    }
    return self;
}

-(void)handleData{
      self.anchorTitleModelArray = [NSMutableArray array];// 初始化大model数组
    
    
      [CCNetTool GET:@"http://mobile.ximalaya.com/mobile/discovery/v1/anchor/recommend?device=iPhone&version=4.3.98" body:nil headerFile:nil response:CCJSON success:^(id result) {
          
//          NSLog(@"%@", result);
          NSDictionary *dic = result;
         
          // 获取famous数组
          NSArray *famous = [dic objectForKey:@"famous"];
          NSArray *famousArray = [CC_AnchorTitle_Model  arrayFromArray:famous];
//          NSLog(@"%ld", famousArray.count);
          
          // 获取normal 数组
          NSArray *normal = [dic objectForKey:@"normal"];
          NSArray *normalArray = [CC_AnchorTitle_Model arrayFromArray:normal];
          
          [self.anchorTitleModelArray  addObjectsFromArray:famousArray];
          [self.anchorTitleModelArray addObjectsFromArray:normalArray];
          
          NSLog(@"%ld", self.anchorTitleModelArray.count);
          
          [self.collectionView reloadData];
          
      } failure:^(NSError *error) {
          NSLog(@"%@", error);
      }];
    
    
    
    
}

-(void)createCollectionView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    
    flowLayout.sectionInset = UIEdgeInsetsMake(SPACED, SPACED, SPACED, SPACED);
    
    
    flowLayout.minimumInteritemSpacing = SPACED;
    flowLayout.minimumLineSpacing = SPACED;

    self.collectionView = [[CCBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64 - 40 - 49) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];  // 颜色
    
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.contentView addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];
    
    // 注册头视图重用池
    [self.collectionView registerClass:[CC_Custom_CollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    
    // 注册尾视图重用池
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];

    // 注册重用池
    [self.collectionView registerClass:[CC_ImageLabel_CollectionViewCell class] forCellWithReuseIdentifier:IMAGELABELCELL];
    
     [self.collectionView registerClass:[CC_AnchorSinger_CollectionViewCell class] forCellWithReuseIdentifier:ANCHORSINGER];
    
    
}

// 区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return self.anchorTitleModelArray.count;
    
    
}

// 每个区item的数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (section <= 3) {
        return 6;
    }else {
        return 3;
    }
    
    
}

// 每个区itemsize的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.section >= 0 && indexPath.section < 3)
    {
        
        return CGSizeMake((SCREENWIDTH - 6 * SPACED) / 3, (SCREENWIDTH - 6 * SPACED) / 3 * 1.7);
        
    }else if (indexPath.section == 3)
    {
        return CGSizeMake(SCREENWIDTH - SPACED * 2, SCREENHEIGHT / 7);
        
    }else
    {
        
         return CGSizeMake((SCREENWIDTH - 6 * SPACED) / 3, (SCREENWIDTH - 6 * SPACED) / 3 * 1.7);
        
    }

    
    
}

// 区头大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
               return CGSizeMake(SCREENWIDTH, 45);
}
// 区尾大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    
    return CGSizeMake(SCREENWIDTH, 15);
}

/// 返回头视图 和 尾部视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    if(kind == UICollectionElementKindSectionHeader){
        
        CC_Custom_CollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header" forIndexPath:indexPath];
        headerView.anchorTitleModel = [self.anchorTitleModelArray objectAtIndex:indexPath.section];
        
        headerView.backgroundColor = [UIColor whiteColor];  // 区头颜色
        
        return headerView;
    }else{
        
        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        footerView.backgroundColor = [UIColor grayColor];  // 区尾颜色
        footerView.alpha = 0.3;
        return footerView;
        
        
    }
    
    
}


-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.section >= 0 && indexPath.section < 3) {
        
        CC_ImageLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IMAGELABELCELL forIndexPath:indexPath];
        
        CC_AnchorTitle_Model *model =  [self.anchorTitleModelArray objectAtIndex:indexPath.section];

        
        cell.anchorDetailedModel = [model.listArray objectAtIndex:indexPath.row];
        
        return cell;
        
    }else if (indexPath.section == 3){
        
          CC_AnchorSinger_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ANCHORSINGER forIndexPath:indexPath];

        CC_AnchorTitle_Model *model =  [self.anchorTitleModelArray objectAtIndex:indexPath.section];
        cell.anchorDetailedModel = [model.listArray objectAtIndex:indexPath.row];
        
    return cell;
 
    }else{
        
        CC_ImageLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:IMAGELABELCELL forIndexPath:indexPath];
        
        CC_AnchorTitle_Model *model =  [self.anchorTitleModelArray objectAtIndex:indexPath.section];
        
        
        cell.anchorDetailedModel = [model.listArray objectAtIndex:indexPath.row];
        
        return cell;
        
    }
    
    
    
}



@end
